<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统-找回密码</div>
            <el-form :model="param" :rules="rules" ref="param" label-width="0px" class="ms-content" status-icon>
                <el-form-item prop="UserName">
                    <el-input v-model="param.UserName" placeholder="UserName" autocomplete="off">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="Password">
                    <el-input type="password" placeholder="Password" v-model="param.Password"
                        @keyup.enter.native="submitForm()" autocomplete="off">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="Password2">
                    <el-input type="password" placeholder="Password" v-model="param.Password2"
                        @keyup.enter.native="submitForm()" autocomplete="off">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="code">
                    <div id="main">
                        <div class="item">
                            <el-col :span="16">
                                <el-input autocomplete="off" placeholder="Password" v-model="identifyCode" @keyup.enter.native="submitForm()">
                                    <el-button slot="prepend" icon="el-icon-tickets"></el-button>
                                </el-input>
                            </el-col>
                            <el-col :span="8">
                                <span class="code" @click="refreshCode">
                                    <IdentifyCode :identify-code="identifyCode" :content-width="96" :content-height="32">
                                    </IdentifyCode>
                                </span>
                            </el-col>
                        </div>
                    </div>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">确定</el-button>
                </div>
                <p class="login-tips">
                    Tips : 用户名和密码随便填。
                    <span style="margin-left: 10%">
                        <router-link to="/login" style="color:darkturquoise">立刻登录</router-link>
                    </span>
                </p>
            </el-form>
        </div>

    </div>
</template>

<script>
import { FindPassword } from '@/api/User.js'
import IdentifyCode from "vue-identifycode";

export default {
    name: "IdentifyCodeTest",
    components: { IdentifyCode },
    data: function () {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.param.Password2 !== '') {
                    this.$refs.param.validateField('Password2');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.param.Password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            param: {
                UserName: '小王',
                Password: '123123',
                Password2: '',
                ValidateCode: '123456',
            },
            rules: {
                UserName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                Password: [{ validator: validatePass, trigger: 'blur' }],
                Password2: [{ validator: validatePass2, trigger: 'blur' }],
            },
            identifyCode: ""
        };
    },
    methods: {
        submitForm() {
            console.log(this.param)
            this.$refs.param.validate(valid => {
                if (valid) {
                    //判断验证码
                    // if(this.identifyCode!=this.param.ValidateCode){
                    //     this.$message.error('验证码错误');
                    // }

                    FindPassword(this.param).then(res => {
                        if (res.Code == 200) {
                            this.$message.success('成功找回密码');
                            //localStorage.setItem('ms_username', this.param.UserName);
                            this.$router.push('/');
                        } else {
                            this.$message.error(res.Message)
                        }
                    })
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        createCode() {
            let code = "";
            const codeLength = 4; // 验证码的长度
            // eslint-disable-next-line no-array-constructor
            const random = new Array(2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F", "G",
                "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
            ); // 随机数
            for (let i = 0; i < codeLength; i++) {
                // 循环操作
                const index = Math.floor(Math.random() * 32); // 取得随机数的索引（0~35）
                code += random[index]; // 根据索引取得随机数加到code上
            }
            return code;
        },
        refreshCode() {
            this.identifyCode = this.createCode();
        }
    },
    mounted() {
        this.identifyCode = this.createCode();
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}

.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}

.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}

.ms-content {
    padding: 30px 30px;
}

.login-btn {
    text-align: center;
}

.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}

.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>
<style scoped>
#main {
    padding: 0px;

    .item {
        position: relative;

        input {
            width: 120%;
            height: 50px;
            padding: 0 0 0 0px;
            border-radius: 2px;
            outline: none;
            border: 1px solid #444;
            font-size: 20px;
        }

        .code {
            position: absolute;
            right: 1px;
            top: 1px;
            display: inline-block;
        }
    }
}
</style> 